﻿using UnityEngine;

namespace Plugins.UFrame
{
    public class PhysicsTool
    {
        public static void IgnoreChildCollider(GameObject go)
        {
            var colAry = go.GetComponentsInChildren<Collider>(true);
            for (int i = 0; i < colAry.Length; i++)
            {
                for (int j = 0; j < colAry.Length; j++)
                {
                    if (i == j)
                        continue;

                    Physics.IgnoreCollision(colAry[i], colAry[j]);
                }
            }
        }

        public static void IgnoreChildCollider2D(GameObject go)
        {
            var colAry = go.GetComponentsInChildren<Collider2D>(true);
            for (int i = 0; i < colAry.Length; i++)
            {
                for (int j = 0; j < colAry.Length; j++)
                {
                    if (i == j)
                        continue;

                    Physics2D.IgnoreCollision(colAry[i], colAry[j]);
                }
            }
        }

        public static void OpenRagDoll(Transform tran)
        {
            var rigAry = tran.GetComponentsInChildren<Rigidbody>();
            for (int i = 0; i < rigAry.Length; i++)
            {
                rigAry[i].velocity = Vector3.zero;
            }
            
            var colliderAry = tran.GetComponentsInChildren<Collider>();
            for (int i = 0; i < colliderAry.Length; i++)
            {
                colliderAry[i].enabled = true;
            }
        }

        


        public static bool RayGroup(Ray[] rayAry, out RaycastHit hit, float distance, int layoutMask,
            bool debug = false)
        {
            hit = new RaycastHit();
            for (int i = 0; i < rayAry.Length; i++)
            {
                bool ray = Physics.Raycast(rayAry[i], out hit, distance, layoutMask);
                if (debug)
                {
                    Debug.DrawRay(rayAry[i].origin, rayAry[i].direction, Color.red, 1f);
                }

                if (ray)
                    return true;
            }

            return false;
        }
    }
}